package com.iamalive;

import java.util.ArrayList;

import org.apache.http.entity.StringEntity;

import com.loopj.android.http.AsyncHttpResponseHandler;

import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;


public class SignupActivity extends Activity {
	
	private EditText mEditTextName;
	private EditText mEditTextUsername;
	private EditText mEditTextPassword;
	private EditText mEditTextConfirmPassword;
	private EditText mEditTextImId;
	private EditText mEditTextPhoneNum;
	private EditText mEditTextAddress;
	private EditText mEditTextCity;
	private EditText mEditTextState;
	private EditText mEditTextCountry;
	private EditText mEditTextZip;
	private Button mButtonSubmit;
	
	private String mName;
	private String mEmailId;
	private String mPassword;
	private String mConfirmPassword;
	private String mImId;
	private String mImService;
	private String mPhoneNum;
	private String mAddress;
	private String mCity;
	private String mState;
	private String mCountry;
	private String mZip;
	private String mDob;
	private String mGender;
	//TODO: Add DOB too
	//TODO: Add gender too
	
	private Context mContext;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_signup);
		
		mContext = this;
		
		mEditTextName = (EditText) findViewById(R.id.editTextSignupName);
		mEditTextUsername = (EditText) findViewById(R.id.editTextSignupUsername);
		mEditTextPassword = (EditText) findViewById(R.id.editTextSignupPassword);
		mEditTextConfirmPassword = (EditText) findViewById(R.id.editTextSignupConfirmPassword);
		mEditTextImId = (EditText) findViewById(R.id.editTextSignupMessengerId);
		mEditTextPhoneNum = (EditText) findViewById(R.id.editTextSignupPhoneNum);
		mEditTextAddress = (EditText) findViewById(R.id.editTextSignupAddress);
		mEditTextCity = (EditText) findViewById(R.id.editTextSignupCity);
		mEditTextState = (EditText) findViewById(R.id.editTextSignupState);
		mEditTextCountry = (EditText) findViewById(R.id.editTextSignupCountry);
		mEditTextZip = (EditText) findViewById(R.id.editTextSignupPostalCode);
		
		mButtonSubmit = (Button) findViewById(R.id.buttonSignupSubmit);
		
		mButtonSubmit.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                onSubmitButtonClicked();
            }
        });
		
	}

	private void onSubmitButtonClicked() {
		mName = mEditTextName.getText().toString().trim();
		mEmailId = mEditTextUsername.getText().toString().trim();
		mPassword = mEditTextPassword.getText().toString().trim();
		mConfirmPassword = mEditTextConfirmPassword.getText().toString().trim();
		mImId = mEditTextImId.getText().toString().trim();
		mPhoneNum = mEditTextPhoneNum.getText().toString().trim();
		mAddress = mEditTextAddress.getText().toString().trim();
		mCity = mEditTextCity.getText().toString().trim();
		mState = mEditTextState.getText().toString().trim();
		mCountry = mEditTextCountry.getText().toString().trim();
		mZip = mEditTextZip.getText().toString().trim();
		//TODO: Add DOB too
		mDob = new String("1981-12-12");
		//TODO: Add gender too
		mGender = "";
				
		if(validateFields() == true) {
			registerNewUser();
		}
	}

	private boolean validateFields() {
		boolean bRetVal = true;
		//bRetVal = false;
		if(mPassword.equals(mConfirmPassword) == false) {
			return false;
		}
		
	
		return bRetVal;
	}
	
	private boolean registerNewUser() {
		boolean bRetVal = true;
		//TODO: Make a web-service call here
		String registerNewUserXml = XmlReaderWriter.assembleRegisterUserRequest(mEmailId, 
				mPassword, mName, mGender, mAddress, mCity, mState, mCountry, mZip, mPhoneNum, mDob);

		StringEntity s = Utils.getStringEntity(registerNewUserXml);

		ImaRestClient restClient = new ImaRestClient();
		restClient.post(mContext, "registeruser", s, new String("text/xml"), new AsyncHttpResponseHandler() {
			private ProgressDialog pd;
			
			@Override
			public void onStart() {
				pd = new ProgressDialog(mContext);
				pd.setTitle("Processing...");
				pd.setMessage("Please wait while I register you..");
				pd.setCancelable(false);
				pd.setIndeterminate(true);
				pd.show();				
            }
			
			@Override
			public void onSuccess(String response) {
				pd.dismiss();
				ArrayList<String> messageList = new ArrayList<String>();
				
				if(true == XmlReaderWriter.parseRegisterUserResponse(response, messageList)) {
					Utils.showToast(mContext, "Registration successful!!! You can login with your email ID and password");
					finish();
				} else {
					StringBuilder messageBuilder = new StringBuilder("Oops...You need to fix these errors:\n");
					for(int i = 0; i < messageList.size(); i++) {
						messageBuilder.append(messageList.get(i)).append("\n");
					}
					Utils.showToast(mContext, messageBuilder.toString());
				}
            }

            @Override
            public void onFailure(Throwable throwable) {
            	pd.dismiss();
            	Utils.showToast(mContext, "Exeption received while registering new user...");
            }
		});
		
		return bRetVal;
	}
	
    /**
     * Called when this activity is about to be destroyed by the system.
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
    }

}
